<template>
  <div class="mod-menu">
    <el-form :inline="true" :model="pamars">
      <el-form-item label="广告名称">
        <el-input v-model="pamars.title" placeholder="广告名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="广告位置">
        <el-select v-model="pamars.mark_code" clearable placeholder="请选择">
          <el-option v-for="item in adLocation" :key="item.mark_code" :label="item.name" :value="item.mark_code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" row-key="Id" border style="width: 100%; ">
      <el-table-column prop="title" align="center" label="广告名称">
      </el-table-column>
      <el-table-column prop="ad_location_name" align="center" label="广告位置">
      </el-table-column>
      <el-table-column prop="model_type" align="center" label="广告类型">
        <template slot-scope="scope">
          <div v-if="scope.row.model_type=='goods'">商品</div>
          <div v-if="scope.row.model_type=='opus'">作品</div>
          <div v-if="scope.row.model_type=='news'">文章</div>
          <div v-if="scope.row.model_type=='service'">服务</div>
          <div v-if="scope.row.model_type=='url'">链接</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="广告图片">
        <template slot-scope="scope">
          　　　　<el-popover placement="top-start" trigger="click">
            <a :href="scope.row.cover" target="_blank" title="查看最大化图片"><img :src="scope.row.cover" style="width: 300px;height: 300px" /></a>
            <img slot="reference" @click="$openImg(scope.row.cover)" :src="scope.row.cover" style="width: 50px;height: 50px; cursor:pointer" />
          </el-popover>
          　　</template>
      </el-table-column>
      <el-table-column prop="sort" align="center" label="排序号">
      </el-table-column>
      <!-- <el-table-column prop="startTime,endTime" align="center" label="时间">
        <template slot-scope="scope">
         <div>开始时间 ：{{scope.row.startTime}}</div>
         <div>结束时间 ：{{scope.row.endTime}}</div>
         </template>
      </el-table-column> -->
      <el-table-column prop="on_off" align="center" label="上线/下线">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.on_off" :active-value="true" :inactive-value="false" inactive-color="#B9B9B9" @change="changeSwitch(scope.row)" />
        </template>
      </el-table-column>

      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="clickTop(scope.row)">{{scope.row.is_top==true?'取消置顶':'置顶'}}</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)" class="c-error">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pamars.page" :page-sizes="[10, 20, 50, 100]" :page-size="pamars.limit" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @updataList='updataList'></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './add-or-update'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      pamars: {
        page: 1,
        limit: 10,
        title: '',
        mark_code: ''
      },
      totalPage: 0,
      dataList: [],
      adLocation: [], // 广告位
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  mounted () {
    this.getList()
    this.getAdLocation()
  },
  methods: {
    // 更新列表
    updataList (e) {
      if (e == 1) {
        this.getList()
      }
    },
    getList () {
      seviceApi.getAdList(this.pamars).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list
          this.totalPage = res.data.page.totalCount
        }
      })
    },
    // 平台广告位查询
    getAdLocation () {
      seviceApi.getAdLocationSelect().then(res => {
        if (res && res.data.code == 200) {
          this.adLocation = res.data.list
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pamars.limit = val
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pamars.page = val
      this.getList()
    },
    // 查询
    getDataList () {
      this.pamars.page = 1
      this.getList()
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 广告上下线
    changeSwitch (e) {
      var param = {
        id: e.id,
        on_off: e.on_off
      }
      seviceApi.postOnOff(param).then(res => {
        if (res && res.data.code == 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getList()
        }
      })
    },
    // 置顶
    clickTop (row) {
      let param = {
        id: row.id,
        is_top: row.is_top == true ? 'false' : 'true'
      }
      seviceApi.postAdTop(param).then(res => {
        if (res && res.data.code == 200) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getList()
        }
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(`确定要删除该条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        seviceApi.delAd(id).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getList()
          }
        })
      }).catch(() => { })
    }
  }
}
</script>
